package com.example.test_load_pic_rough;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.cherry.lib.doc.DocViewerActivity;
import com.cherry.lib.doc.bean.DocEngine;
import com.cherry.lib.doc.util.Constant;
import com.cherry.lib.doc.widget.DocView;


public class KfdDocViewerActivity extends DocViewerActivity {

    private static Intent intent;
    private String docUrl;
    private int docSourceType;
    private int fileType;
    private int engine;
    private static final String TAG = "DocViewerActivity";
    private DocView mDocView;

    // 静态方法用于启动 DocViewerActivity
    public static void launchDocViewer(AppCompatActivity activity, int docSourceType, String path, Integer fileType, Integer engine) {
        // 注意：fileType 和 engine 参数在 Kotlin 中是可选的，但在 Java 中必须是明确的，或者我们可以为它们提供默认值（通过方法重载或使用 null 检查）
        intent = new Intent(activity, KfdDocViewerActivity.class);
        intent.putExtra(Constant.INTENT_SOURCE_KEY, docSourceType);
        intent.putExtra(Constant.INTENT_DATA_KEY, path);
//        String fileName = FileUtil.getFileName(path);
//        Bundle bundle = new Bundle();
//        bundle.putString("fileName" ,fileName);
        if (fileType != null) {
            intent.putExtra(Constant.INTENT_TYPE_KEY, fileType);
        }
        if (engine != null) {
            intent.putExtra(Constant.INTENT_ENGINE_KEY, engine);
        }
        activity.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kfd_doc_viewer);

        mDocView = findViewById(R.id.mDocView);
        ImageView ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(v -> finish());

        //String fileName = intent.getStringExtra("fileName");
//        TextView tvFileName = findViewById(R.id.tv_file_name);
//        tvFileName.setText(fileName);

        //调用父类的方法打开文件
        initData(intent);
    }


    public void initData2(Intent intent) {
        // 从Intent中提取数据，处理可能的null值
        if (intent != null) {
            docUrl = intent.getStringExtra(Constant.INTENT_DATA_KEY);
            docSourceType = intent.getIntExtra(Constant.INTENT_SOURCE_KEY, 0);
            fileType = intent.getIntExtra(Constant.INTENT_TYPE_KEY, -1);
            engine = intent.getIntExtra(Constant.INTENT_ENGINE_KEY, DocEngine.INTERNAL.getValue());
        } else {
            // 如果intent为null，则设置默认值
            docUrl = null;
            docSourceType = 0;
            fileType = -1;
            engine = DocEngine.INTERNAL.getValue();
        }

        // 假设mDocView已经初始化，并且openDoc方法接受这些参数
        // 注意：Java中枚举不能直接通过值比较来获取枚举实例，需要遍历或使用Map来映射值到枚举实例
        DocEngine docEngine = null;
        for (DocEngine e : DocEngine.values()) {
            if (e.getValue() == engine) {
                docEngine = e;
                break;
            }
        }
        // 如果DocEngine的value是唯一的，也可以使用一个Map来存储value到DocEngine的映射，以提高查找效率
        // Map<Integer, DocEngine> engineMap = new HashMap<>();
        // ... 初始化engineMap
        // docEngine = engineMap.get(engine);

        if (mDocView != null) {
            mDocView.openDoc(this, docUrl, docSourceType, fileType, false, docEngine);
        } else {
            Log.e(TAG, "mDocView为null!!!!!!!!!!!!!!!!!!! = ");
        }

        // 打印日志
        Log.e(TAG, "initData-docUrl = " + docUrl);
        Log.e(TAG, "initData-docSourceType = " + docSourceType);
        Log.e(TAG, "initData-fileType = " + fileType);
        Log.e(TAG, "initData-engine = " + engine);
    }


//    private void showPdf(File file , PdfQuality pdfQuality) {
//        Rect rect = new Rect(0, 0, 0, 0);
//        PdfRendererCore pdfRendererCore = new PdfRendererCore(this, file, pdfQuality)
//        pdfViewAdapter = PdfViewAdapter(pdfRendererCore, pageMargin, true,this)
//        pdfPageViewAdapter = PdfPageViewAdapter(pdfRendererCore, pageMargin, enableLoadingForPages)
//        mRvPdf.setEnableScale(true)
//
//        mRvPdf.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
//        mRvPdf.adapter = pdfViewAdapter
//    }
}